package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Product;
import com.example.common.util.R;
import com.example.buildtable.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "产品分类")
@RequestMapping(value = "/product/")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Operation(summary = "查询所有产品分类数据")

    @GetMapping(value = "/getAllProduct")
    public R getAllProduct() {
        List<Product> productList = productService.list();
        return R.ok("查询所有产品分类数据成功").data("productList", productList);
    }

    @Operation(summary = "查询所有产品分类数据分页")
    @PostMapping(value = "/getProductPage/{page}/{limit}")
    public R getProductPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        LambdaJoinQueryWrapper<Product> wrapper = new LambdaJoinQueryWrapper<>(Product.class);
        Page<Product> productPage = new Page<>(page, limit);
        productService.page(productPage, wrapper);
        //每页显示的记录
        List<Product> productList = productPage.getRecords();
        //共多少条记录
        Long total = productPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", productList);
        return R.ok("查询所有产品分类数据分页成功").data("map", map);
    }
    
    @Operation(summary = "增加产品分类")
    @PostMapping(value = "/addProduct")
    public R addProduct(@RequestBody Product product) {
        boolean flag = productService.save(product);
        if (flag) {
            return R.ok("增加产品分类成功");
        } else {
            return R.ok("增加产品分类失败");
        }
    }

    @Operation(summary = "删除产品分类")
    @GetMapping(value = "/deleteProduct/{productId}")
    public R deleteProduct(@PathVariable("productId") Integer productId) {
        boolean flag = productService.removeById(productId);
        if (flag) {
            return R.ok("删除产品分类成功");
        } else {
            return R.ok("删除产品分类失败");
        }
    }

    @Operation(summary = "按照产品分类查看")
    @GetMapping(value = "/getProductById/{productId}")
    public R getProductById(@PathVariable("productId") Integer productId) {
        Product product = productService.getById(productId);
        if (product != null) {
            return R.ok("按照产品分类查看成功").data("product", product);
        } else {
            return R.error("按照产品分类查看失败");
        }
    }

    @Operation(summary = "修改产品分类")
    @PostMapping(value = "/updateProduct")
    public R updateProduct(@RequestBody Product product) {
        boolean flag = productService.updateById(product);
        if (flag) {
            return R.ok("修改产品分类成功");
        } else {
            return R.ok("修改产品分类失败");
        }
    }

    @Operation(summary = "修改分类的状态")
    @PostMapping(value = "/updateProductStatus")
    public R updateProductStatus(@RequestBody Product product) {
        Integer status = product.getProductStatus();
        if (status == 1 || status == 0) {
            int newStatus = (status == 1) ? 0 : 1;
            product.setProductStatus(newStatus);
            boolean updated = productService.updateById(product);

            String statusText = null;
            if (updated) {
                statusText = (newStatus == 1) ? "是" : "否";
                return R.ok(statusText + product.getProductName() + "成功").data("product", product);
            } else {
                return R.error(statusText + product.getProductName() + "失败");
            }
        } else {
            return R.error("无效的状态值");
        }
    }
}